import { combineResolvers } from 'graphql-resolvers'
import { pageModel } from '../../lib/helper'
import auth from '../../middlewares/auth'
import { userLoader } from '../user/dataloaders'
import { commentLoaderByAnswer } from '../comment/dataloaders'

module.exports = {
  Query: {
    // answer: async (root, { id }, { models: { Answer } }) => {
    //   const result = await Answer.findById(id)
    //   return result
    // },
    // progressAnswers: async (root, { progressId }, { models: { Answer } }) => {
    //   // const result = await $answer.findProgressAnswer(progressId)
    //   const result = await Answer.findAll({ where: { progressId } })
    //   return result
    // }
  },
  Mutation: {
    createAnswer: combineResolvers(
      auth,
      async (root, { input }, { services: { $answer }, user }) => {
        input.userId = user.id
        console.info('create answer:', input)
        return await $answer.createOrUpdate(input)
      }
    )
    // removeAnswer: combineResolvers(
    //   auth,
    //   async (root, { id }, { services: { $answer }, user }) => {
    //     let userId = user.id
    //     console.info('remove answer:', id)
    //     return await $answer.remove(id, userId)
    //   }
    // )
  },
  Answer: {
    user: ({ userId }, arg, { dataloaders: { userLoader } }) => {
      return userLoader.load(userId)
    },
    comments: ({ id }, arg, { dataloaders: { commentLoaderByAnswer } }) => {
      return commentLoaderByAnswer.load(id)
    },
    liked: ({ id }, data, { dataloaders: { likedLoaderByAnswer } }) => {
      return likedLoaderByAnswer.load(id)
    }
  }
}
